Skip to content

Decode cached facts only when they are read (#145 M3) - #333

Merged
QuintinBotes merged 5 commits into
mainfrom
perf/lazy-extraction-145
Nov 30, 2026
Merged

QuintinBotes merged 5 commits into
mainfrom
perf/lazy-extraction-145

Conversation

@QuintinBotes

Copy link
Copy Markdown
Owner

Advances #145 (milestone M3).

Stacked on #332 (perf/incremental-python-link-refs-145). I will rebase onto main once #332 merges.

A map no longer decodes every cached extraction while it validates the cache. Each file's facts are decoded when something asks for them. Every step that still reads every file's facts gets them from one function, allFileFacts(), and M4 replaces it.

What changed

  • The extraction phase reads validity metadata only. A valid paged cache row is kept as a reference (page and slot). Its page is inflated and its JSON parsed only when that file's facts are read (lazyExtractions in runtime/state/extraction-cache.mjs).
    • Unpaged rows are still decoded at lookup, because the existing migration into pages needs their JSON. These are rows from older releases, the previous map's small batches, and oversized results.
    • If another writer replaces a row before it is read, the row is read again when its key is unchanged. Otherwise the file is reported as a failure, so the map is partial.
  • allFileFacts() is the isolated consumer. It decodes every remaining reference in page order, so each page is inflated once. It returns the same path-ordered Map that perFile was, with the census is_test rule applied.
    • Linking, discovery, the canonical-id rewrite, the dependency and import matchers, and projection all take it unchanged.
    • Sparse-page repacking now happens here, after the decode.
  • Coverage is counted from kept per-file summaries. For each source file the summary holds the adapter of its module fact, its parse quality, and the analyses that ran and could run.
    • Summaries are worked out before linking, from the extracted facts. They are kept in one brotli row (file_index, adapter coverage:files), keyed by the link key and by each file's blob, kind, test flag and language.
    • A re-map decodes only files whose key changed, plus files extracted or failed in this map.
    • SCIP's parse_quality: 'semantic' mutation is applied from the semantic map.
    • languageCoverage and analysisCoverage take the summaries.
    • Checked during development: the old post-link computation and the new one gave identical coverage and analysis on every map in npm test. That temporary assertion is not committed.
  • New counters and phase.
    • facts_read: { files, facts, pages, all: { files, facts, pages } } in the map summary. The top level is what lazy reads decoded; all is what allFileFacts() decoded.
    • A new all_facts_ms phase, which bench.mjs also reports.
  • moduleBy, the eager addFacts, and the census-order and path-order re-sorts are gone.

Audit of perFile consumers

Consumer Now
cache validation, empty-cache and migration bookkeeping metadata only, O(files × adapters) row lookups
is_test rule, path order applied on read / in allFileFacts()
coverage, analysis coverage kept summaries; decodes changed files only
fast-path key, census cache, history, derived, stats never read facts (unchanged)
link state (inputs, previousFacts) keys only, plus stale rows (unchanged)
every linker (factsByFile) allFileFacts(): JS and Python have global passes over every file (frameworks, member reach, symbol indexes); the others scan every file
discovery (literals), canonical ids, dependency/runtime/stats/delivery imports allFileFacts()
projection (run hashing, merge pass) allFileFacts() until M4

Compatibility. No migration and no schema change. Older releases ignore the coverage:files row. A store without that row, or with a different key, works its summaries out once. node scripts/upgrade-test.mjs --count 2 (v0.3.3, v0.4.0): 2/2 passed.

Measurements

Deterministic counters, bench 10k fixture, the bench's 10-file edit:

map lazy files / facts / pages all.files / all.facts / all.pages
10-file edit 0 / 0 / 0 9,992 / 85,173 / 352
10-file edit, after a build without this change mapped 3 commits 29 / 317 / 6 9,963 / 85,503 / 420

The edited files' facts were all freshly extracted, so lazy reads decoded nothing. After a build without this change mapped three 10-file commits, the kept summaries of the 29 distinct files those commits touched were stale, and exactly those were decoded.

Matched bench, node scripts/bench.mjs --sizes 10k, alternating origin/main and this branch. The branch includes #332. Times are results[0].incremental, in ms.

run load before (1/5/15) wall extraction all_facts link projection
main 1 9.84 17.11 14.04 1,055 319 – 289 202
branch 1 7.85 16.05 13.75 1,242 157 189 429 248
main 2 7.18 15.22 13.51 1,139 333 – 345 233
branch 2 7.60 14.66 13.36 1,160 149 187 399 225

50k, one run per side:

run load before wall extraction all_facts link coverage projection
main 6.00 8.86 11.00 5,958 1,570 – 2,114 292 1,119
branch 7.31 8.48 10.53 6,173 589 1,238 2,302 46 1,113

This PR does not make a re-map faster, as expected for M3 alone. The decode moves out of extraction into allFileFacts():

  • At 50k, extraction plus all_facts plus coverage is 1,873 ms against 1,862 ms on main.
  • At 10k it is about 340 ms, against about 325 ms on main (extraction alone).
  • link_ms is 50–100 ms higher on the branch in every run. An extra round of two runs against Link again only the Python modules a change reaches (#145) #332's branch alone gave the same picture: link 405 against 328, and 543 against 425 ms. CPU profiles over four edits each show no new link-side work; they differ by about 27 ms per edit in total, of which coverage summaries are about 10–15 ms. I read the rest as GC and load noise (load average 5–17), but I have not proven it.

What still scales with the repository

  • allFileFacts(): 1.24 s at 50k, about 0.19 s at 10k. This is the cost M4 must remove, together with making the linkers' global passes work from the change set.
  • Cache validation: 0.59 s at 50k, about 0.15 s at 10k. About 300k metadata lookups (files × adapters). Taking unchanged files from the census diff against the last map's inputs would remove these lookups.
  • The coverage-summary row is decoded and, after any change, rewritten whole: about 9 ms at 10k (107 KB packed, about 12 bytes per file), so roughly 0.5 MB at 50k.

Tests

  • remap-cost.test.mjs: a 60-file project with a 10-file edit.
    • The cold map decodes nothing.
    • The edit decodes at most 10 files lazily, and allFileFacts() decodes the other 50 or more.
    • coverage and analysis equal a cold map's of the edited tree.
  • extraction-pages.test.mjs, ported to the lazy reader:
    • a sparse page's member is listed for repack and then released;
    • members whose page another writer reclaimed are read again;
    • a member whose key changed is reported lost.
  • Cold equals incremental: incremental-*.test.mjs and the JS and Python randomized differential tests pass unchanged.
  • npm test: 4,017 tests, 4,007 pass, 0 fail, 10 skipped.

🤖 Generated with Claude Code

Quintin Botes and others added 2 commits November 30, 2026 13:03
The Python linker emits its output per module and records what each
module's link read in link_refs: the files whose symbols or bindings it
looked up, and the layout keys its resolution read (mod:<dotted>,
top:<package>, stem:<path>). A re-map links again the changed modules,
the modules that read a layout key of a Python file that came or went,
the modules that read either, and modules with unused-member evidence;
every other module reuses its kept output. Different source roots, an
empty link_refs or a semantic index link every module.

directories() and packFact/unpackFact move to link-state.mjs and are
shared with the JavaScript linker; a packed `path:line` source ref is
stored as the line number. The randomized test harness moves to
tests/helpers/incremental-link.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…aries (#145 M3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from perf/incremental-python-link-refs-145 to main November 30, 2026 12:36
@QuintinBotes
QuintinBotes merged commit 8535a67 into main Nov 30, 2026
12 of 13 checks passed
@QuintinBotes
QuintinBotes deleted the perf/lazy-extraction-145 branch November 30, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant